// pages/game/game.js
let touchDotX = 0;//X按下时坐标
let touchDotY = 0;//y按下时坐标
let countPull = 0

Page({

  /**
   * 页面的初始数据
   */
  data: {
    showModal: false,
    showBling: false,
    hidden: true,
    moneyList: [
      { show: true, animation: null, zIndex: 10 }
    ],
    score: 0, //分数
    tamp: 720, //倒计时总时间
    tampStr: '00:00:00',
    animation: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let animation = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease-in'
    });
    animation.opacity(.6).top(-100)
    this.setData({
      animation
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 回到游戏
   */
  goBack() {
    this.initGameData()
  },
  initGameData() {
    this.setData({
      showModal: false,
      moneyList: [{ show: true, animation: null, zIndex: 10 }],
      hidden: true,
      tamp: 720
    })
  },
  /**
   * 跳转到排行榜
   */
  goRank() {
    wx.navigateTo({
      url: '/pages/gameDetail/gameDetail',
      success: ()=> {
        this.initGameData()
      }
    })
  },
  // 触摸开始事件
  touchStart: function(e) {
    touchDotX = e.touches[0].pageX; // 获取触摸时的原点
    touchDotY = e.touches[0].pageY;
  },
  // 触摸结束事件
  touchEnd: function(e) {
    let touchMoveX = e.changedTouches[0].pageX;
    let touchMoveY = e.changedTouches[0].pageY;
    let tmX = touchMoveX - touchDotX;
    let tmY = touchMoveY - touchDotY;
    // if (time < 20) {
    let absX = Math.abs(tmX);
    let absY = Math.abs(tmY);
    if (absX > 2 * absY) {
      if (tmX<0){
        console.log("左滑=====")
      }else{
        console.log("右滑=====")
      }
    }
    if (absY > absX * 2 && tmY<0) {
      let arr = this.data.moneyList
      arr.push({show: true})
      this.setData({
        moneyList: arr
      })
      
      this.execAnimation(countPull)
      countPull++
    }
    // }
  },
  /**主动画执行 */
  execAnimation(index) {
    let animation = this.data.animation.step()
    
    let attrIndex = "moneyList[" + index + "].animation";
    let zIndex = "moneyList[" + index + "].zIndex";
    this.setData({
      showBling: true,
      [attrIndex]: animation.export(),
      [zIndex]: 66 + countPull
    })
    let timer = setTimeout(() => {
      let attrIndex = "moneyList[" + index + "].show";
      this.setData({
        [attrIndex]: false
      })
      clearTimeout(timer)
      timer = null;
    }, 500)
    this.execMoneyAnimation()
  },
  /**撒钱动画执行 */
  execMoneyAnimation() {
    let timer = setTimeout(() => {
      this.setData({
        showBling: false
      })
      clearTimeout(timer)
      timer = null;
    }, 550)
  },
  startGame() {
    this.setData({
      hidden: false
    })
    this.countTime()
  },
  /**倒计时 */
  countTime() {
    let h = ''
    let m = ''
    let s = ''

    if(this.data.tamp > 0) {
      h = Math.floor(this.data.tamp/60/60/60%24)
      m = Math.floor(this.data.tamp/60/60%60)
      s = Math.floor(this.data.tamp/60%60)
    } else {
      // 时间到后，把数据回到初始状态
      let score = this.data.moneyList.filter(item => !item.show).length;
      this.setData({
        score: score,
        hidden: true,
        moneyList: [{ show: true, animation: null, zIndex: 10 }],
        showModal: true
      })
      countPull = 0;
      return false
    }
    let tamp = this.data.tamp - 60
    this.setData({
      tamp,
      tampStr: `${h<10? '0'+h : h}:${m<10? '0'+m : m}:${s<10? '0'+s : s}`
    })
    setTimeout(this.countTime, 1000)
  }
})